package com.ama.controller;

import com.aliyuncs.http.HttpRequest;
import com.ama.pojo.Property;
import com.ama.pojo.Staff;
import com.ama.service.PropertyService;
import com.ama.service.StaffService;
import com.ama.utils.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@Controller
public class LoginController {
    @Autowired
    private StaffService staffService ;
    @Autowired
    private PropertyService propertyService;
    String code = "";

    @RequestMapping("/loginConfirm")
    public String LoginDo(Model model , HttpSession session, HttpServletRequest request) throws IOException {

        Long username = null;
        String password = null;
        String person = null;
        try {
            username = Long.valueOf(request.getParameter("username"));
            System.out.println(username);
            password = (String)request.getParameter("password");
            System.out.println(password);
            person = (String)request.getParameter("person");
            System.out.println(person);

        //员工/物业管理员判断
        if(person!=null&&person.equals("staff")){
            //员工：
            if(staffService.selectByIdAndPassword(username,password)!=null){
                Staff staff = staffService.selectByIdAndPassword(username,password);
                String phone4 = staff.getStaffPhone();
//               code  = PhoneCode.getPhonemsg(phone4);
                code="123456";
                request.setAttribute("code",code);
                request.setAttribute("phone",phone4.substring(7));
                session.setAttribute("user",staff);
                return "login_noteservice";

            }else{
                return "redirect:/error/login_error.jsp";
            }
        }else {
            //物业管理员：
            if(propertyService.selectByIdAndPassword(username,password)!=null){
                Property property = propertyService.selectByIdAndPassword(username,password);
                String phone4 = property.getPropertyPhone();
//                code  = PhoneCode.getPhonemsg(phone4.substring(7));
                code = "123456";
                request.setAttribute("code",code);
                request.setAttribute("phone",phone4.substring(7));
                session.setAttribute("user",property);
                return "login_noteservice";

            }else{
                System.out.println("id/password错误！");
                return "redirect:/error/login_error.jsp";
            }
        }
        } catch (NumberFormatException e) {
            return "redirect:/error/login_error.jsp";
        }
    }


    @RequestMapping("/codeConfirm")
    public String codeConfirm(HttpServletRequest request,HttpSession session){

        String codeConfirm = (String)request.getParameter("code");
        if(code.equals(codeConfirm)){
            //登录成功
            if(session.getAttribute("user").getClass().getName().equals(Staff.class.getName())){
                return "redirect:/staff_page/s_index.jsp";
            }else {
                return "redirect:/pro_page/p_index.jsp";
            }
        }else {//登录失败，重定向到登录页面
            System.out.println("验证码错误！");
            return "redirect:/error/login_error.jsp";
       }
    }

    @RequestMapping("/forgetPsw")
    public String forgetPsw(HttpServletRequest request,HttpSession session){
        Long username = null;
        String person = null;
        try {
            username = Long.valueOf(request.getParameter("username"));
            System.out.println(username);
            person = (String)request.getParameter("person");
            System.out.println(person);
        } catch (NumberFormatException e) {
            return "redirect:/error/login_error.jsp";
        }
        //员工/物业管理员判断
        if(person.equals("staff")){
            //员工：
            if(staffService.selectById(username)!=null){
                Staff staff = staffService.selectById(username);
                String phone4 = staff.getStaffPhone();
//               code  = PhoneCode.getPhonemsg(phone4);
                code="123456";
                request.setAttribute("code",code);
                request.setAttribute("phone",phone4.substring(7));
                session.setAttribute("user",staff);
                return "forgetPsw_noteservice";

            }else{
                return "redirect:/error/login_error.jsp";
            }
        }else {
            //物业管理员：
            if(propertyService.selectById(username)!=null){
                Property property = propertyService.selectById(username);
                String phone4 = property.getPropertyPhone();
//                code  = PhoneCode.getPhonemsg(phone4.substring(7));
                code = "123456";
                request.setAttribute("code",code);
                request.setAttribute("phone",phone4.substring(7));
                session.setAttribute("user",property);
                return "forgetPsw_noteservice";

            }else{
                System.out.println("id/password错误！");
                return "redirect:/error/login_error.jsp";
            }
        }
    }

    @RequestMapping("/forgetNoteConfirm")
    public String forgetNoteConfirm(HttpServletRequest request,HttpSession session){
        String newPassword = null;
        String codeConfirm = null;
        try {
            newPassword = request.getParameter("newPassword");
            codeConfirm = (String)request.getParameter("code");
        } catch (Exception e) {
            return "redirect:/error/login_error.jsp";
        }
        if(code.equals(codeConfirm)){
            //登录成功
            if(session.getAttribute("user").getClass().getName().equals(Staff.class.getName())){
                Staff staff = (Staff) session.getAttribute("user");
                staff.setStaffPassword(newPassword);
                staffService.updateByIdSelective(staff);
                session.setAttribute("user",staff);
                return "redirect:/staff_page/s_index.jsp";
            }else {
                Property property = (Property)session.getAttribute("user");
                property.setPropertyPassword(newPassword);
                propertyService.updateByIdSelective(property);
                session.setAttribute("user",property);
                return "redirect:/pro_page/p_index.jsp";
            }
        }else {//登录失败，重定向到登录页面
            System.out.println("验证码错误！");
            return "redirect:/error/login_error.jsp";
        }
    }

}
